package com.gitee.wsl.transform.encode.base


import kotlin.experimental.and

object Base2 {
    fun encode(data: ByteArray): String {
        val out = StringBuilder()
        for (b in data) {
            for (j in 0 until 8) {
                if (b and (1 shl (7 - j)).toByte() == 0u.toByte()) {
                    out.append('0')
                } else {
                    out.append('1')
                }
            }
        }
        return out.toString()
    }

    fun decode(data: String): ByteArray {
            val vdata = if ((data.length and 7) != 0) {
                // prepend the padding
                "0".repeat(8 - (data.length and 7)) + data
            } else {
                data
            }
            val out = ByteArray(vdata.length shr 3)
            for ((dstIndex, i) in (vdata.indices step 8).withIndex()) {
                val value = vdata.subSequence(i,i + 8).toString().toInt(2)
                //val value = Integer.parseInt(vdata, i, i + 8, 2)
                out[dstIndex] = value.toByte()
            }
            return out

    }
}

fun ByteArray.encodeBase2() = Base2.encode(this)

fun String.decodeBase2() =  Base2.decode(this)
